<!DOCTYPE html>
<html class="x-admin-sm">
{include file='public/header'}

<body>
    <div class="x-body">
        <blockquote class="layui-elem-quote" style="border-color:#ff5722;color: #db552b;">请勿输入特殊符号！</blockquote>
        <form class="layui-form" lay-filter="formAjax">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    平台类型
                </label>
                <div id="pMenu"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>充值类型
                </label>

                <div class="layui-input-block" id="type"></div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>充值金额名
                </label>
                <div class="layui-input-block">
                    <input type="text" name="name" id="name" placeholder="充值金额名" required="required"
                        lay-verify="required|ZnNum" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    标签
                </label>
                <div class="layui-input-block">
                    <input type="text" name="label" id="label" placeholder="标签" autocomplete="off" lay-verify="ZnNum" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>展示对象
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="display_object" value="1" title="全部用户" checked>
                    <input type="radio" name="display_object" value="2" title="已充值用户">
                    <input type="radio" name="display_object" value="3" title="未充值用户">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>充值金额
                </label>
                <div class="layui-input-block">
                    <input type="number" name="recharge_price" id="recharge_price" placeholder="充值金额" required="required"
                        lay-verify="not_zero" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>赠送币数
                </label>
                <div class="layui-input-block">
                    <input type="number" name="send_currency" id="send_currency" placeholder="赠送币数" required="required"
                        lay-verify="required|num" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>赠送标语
                </label>
                <div class="layui-input-block">
                    <input type="text" name="send_title" id="send_title" placeholder="赠送标语" required="required"
                        lay-verify="required|ZnNum" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>排序
                </label>
                <div class="layui-input-block">
                    <input type="number" name="sort" id="sort" placeholder="排序" autocomplete="off" class="layui-input" value="" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>是否用于公共配置
                </label>
                <div class="layui-input-block">
                    <input type="radio" name="is_common" value="0" title="否" checked>
                    <input type="radio" name="is_common" value="1" title="是">
                </div>
            </div>

            <input type="hidden" name="id" id="id" value="" />
            <button id="submitData" class="layui-btn" lay-submit lay-filter="submitData" style="display: none;" />

        </form>
    </div>
    <script>
        let params = {
            addUrl: '/admin/money/add',
            editUrl: '/admin/money/edit',
            detailUrl: '/admin/money/detail',
        }
        form_ajax(params)

        let selectParams = {
            url:'/admin/money/getPlatformType',
            elem: '#pMenu',
            selected: [],
            tips: '选择平台类型',
            name: 'platform_type',
            field_id:'type',
            field_Name:'name',
        }
        let id = GetQueryString('id')
        
        layui.use('form', function(){ 
            var form = layui.form;
            form.verify({
                not_zero: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value == ''){
                        return '金额不能为空！';
                    }
                    if(Number(value) <= 0){
                        return '金额不能为0！';
                    }
                },
                ZnNum: function(value, item){ //value：表单的值、item：表单的DOM对象
                    var regEn = /[`!@#$%^&*()_<>?:"{}\/;'[\]]/im,
                        regCn = /[·！#￥（——）：；“”‘|《。》？【】[\]]/im;
                    if (regEn.test(value) || regCn.test(value)) {
                        return '请勿输入特殊符号！';
                    }
                }
                ,num: [
                    /^[0-9]*$/
                    ,'只能输入数字！'
                ] 
                
            });
            Ajax('/admin/money/getRechargeType', {type:1}).then(data => {
                if (id) {
                    Ajax(params.detailUrl, { id }).then(result => {
                        selectParams.selected = result.data.platform_type?.toString() ?? []
                        selectN(selectParams)
                        setTypeTemplate(data.data,result.data.type)
                    });
                } else {
                    selectN(selectParams)
                    setTypeTemplate(data.data)
                }
            })

            function setTypeTemplate(RechargeType,type = 1){
                let type_template = '';
                RechargeType.forEach(item=>{
                    type_template += `<input type="radio" name="type" value="${item.id}" title="${item.name}" ${item.id == type ? 'checked' : ''}>`
                })
                $('#type').html(type_template)
                form.render()
            }
        });
        $("body").on("keypress", "input", function (e) {
            if(e.which === 32 || e.which == 13){
                return false;
            }
        });

        
    </script>
</body>

</html>